package com.lianliankan.util;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;

import android.content.Context;
import android.graphics.Color;
import android.os.AsyncTask;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;

import com.example.llkan.R;
import com.lianliankan.model.Score;

public class RankAsyncTask extends AsyncTask<Object, Object, LinkedList<Score>> {

	private ListView listview;
	private TextView liveText;
	private TextView moneyText;
	private TextView dimondText;
	private Context context;

	public RankAsyncTask(TextView liveText, TextView moneyText,
			TextView dimondText, ListView listview, Context context) {
		this.liveText = liveText;
		this.moneyText = moneyText;
		this.listview = listview;
		this.context = context;
		this.dimondText = dimondText;
	}

	@Override
	protected LinkedList<Score> doInBackground(Object... params) {
		String rankJson;
		HttpHelper httpHelper = new HttpHelper();
		NameValuePair nameValuePair = new BasicNameValuePair("account", "user");
		List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
		nameValuePairs.add(nameValuePair);
		rankJson = httpHelper.httpGetRank(nameValuePairs, "index.aspx");
		// rankJson = rankJson.substring(rankJson.indexOf("["),
		// rankJson.indexOf("]") + 1);
		JsonPase jsonPase = new JsonPase();
		LinkedList<Score> scorelist = jsonPase.JsonPaser(rankJson);
		return scorelist;
	}

	@Override
	protected void onPostExecute(LinkedList<Score> scorelist) {
		SimpleAdapter adapter = new SimpleAdapter(context, getData(scorelist),
				R.layout.main, new String[] { "ranknum", "img", "nickname",
						"playscore" }, new int[] { R.id.ranknum, R.id.img,
						R.id.nickname, R.id.playscore });
		liveText.setText(String.valueOf(GlobalValue.currentUser
				.getBenevolence()));
		moneyText.setText(String.valueOf(GlobalValue.currentUser.getMoney()));
		dimondText
				.setText(String.valueOf(GlobalValue.currentUser.getDiamond()));
		liveText.setTextColor(Color.WHITE);
		moneyText.setTextColor(Color.WHITE);
		dimondText.setTextColor(Color.WHITE);
		listview.setAdapter(adapter);
	}

	private List<Map<String, Object>> getData(LinkedList<Score> scorelist) {
		List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
		Collections.reverse(scorelist);
		int i = 1;
		for (Iterator<Score> iterator = scorelist.iterator(); iterator
				.hasNext();) {
			Score score = iterator.next();
			if (String.valueOf(score.getUser().getAccount()).equals(
					GlobalValue.account)) {
				GlobalValue.currentUser = score.getUser();
			}
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("ranknum", i++);
			map.put("img", R.drawable.ic_launcher);
			map.put("nickname", "lv"
					+ levelCalculate(score.getUser().getExperience()) + "  "
					+ score.getUser().getNickName());
			map.put("playscore", score.getPlayScore());
			list.add(map);

		}
		return list;
	}

	private int levelCalculate(int exp) {
		int baseExp = 150;
		int lv = 1;
		while (true) {
			if (exp < baseExp) {
				return lv;
			}
			baseExp = baseExp * 2;
			lv++;
		}
	}
}